package com.wecon.springdemo.controller;

import com.wecon.springdemo.bean.Myuser;
import com.wecon.springdemo.ejob.MyJob;
import com.wecon.springdemo.pattern.template.order.Application;
import com.wecon.springdemo.pattern.template.order.CompanyA;
import com.wecon.springdemo.pattern.template.order.CompanyB;
import com.wecon.springdemo.pattern.template.order.CompanyC;
import com.wecon.springdemo.service.MyuserService;
import com.wecon.springdemo.test.async.AsyncTaskTest;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.elasticjob.lite.api.bootstrap.impl.OneOffJobBootstrap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/**
 * @author Weikang  Wang
 * @program: com.wecon.springdemo.controller
 * @description:
 * @date 2020/4/10 11:23
 */
@RestController
@RequestMapping("/test")
@Slf4j
public class TestController {

    @Autowired
    private MyuserService myuserService;

    @Autowired
    private AsyncTaskTest asyncTaskTest;

    @RequestMapping(value = "/hello")
    public String test() {
        log.info("TestController被调用了");
        return "Hello World";
    }

    @RequestMapping(value = "/getUserInfo")
    public Myuser getUserInfo(@RequestParam("userId")Integer userId) {
        return myuserService.getMyuserInfo(userId);
    }

    @RequestMapping(path = "/testThymeleaf")
    public ModelAndView testThymeleaf() {

        String name = "Weikang Wang";
        ModelAndView modelAndView = new ModelAndView("/index.html");
        modelAndView.addObject("name", name);

        return modelAndView;
    }

/*    @RequestMapping("/template")
    public void templateTest() {
        CompanyA companyA = new CompanyA();
        companyA.insertData(Application.jsonA);

        CompanyB companyB = new CompanyB();
        companyB.insertData(Application.jsonB);

        CompanyC companyC = new CompanyC();
        companyC.insertData(Application.jsonC);
    }*/

    @RequestMapping(value = "/job")
    public String startMyJob() {
        Logger logger = LoggerFactory.getLogger(TestController.class);
        logger.info("startMyJob被调用了");

//        oneOffJobBootstrap.execute();

        return "Hello World";
    }

    @GetMapping("/asyncTask")
    public void testAsyncTask() {
        try {
            asyncTaskTest.doTaskOne();
            asyncTaskTest.doTaskTwo();
            asyncTaskTest.doTaskThree();
        } catch (Exception e) {
            log.error("执行异常");
            e.printStackTrace();
        }
    }

}
